package cn.iocoder.yudao.framework.validation;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.StrUtil;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * @author 山野羡民
 * @since 2025/01/18
 * @see cn.iocoder.yudao.framework.common.validation.MobileValidator
 */
public class PhoneNumberValidator implements ConstraintValidator<PhoneNumber, String> {

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // 如果输入为空，默认不校验，即校验通过
        if (StrUtil.isBlank(value)) {
            return true;
        }
        // 校验是电话号码，支持中国大陆手机号、港澳台手机号、固话
        return cn.hutool.core.lang.Validator.isMatchRegex(PatternPool.MOBILE, value)
                || cn.hutool.core.lang.Validator.isMatchRegex(PatternPool.MOBILE_HK, value)
                || cn.hutool.core.lang.Validator.isMatchRegex(PatternPool.MOBILE_MO, value)
                || cn.hutool.core.lang.Validator.isMatchRegex(PatternPool.MOBILE_TW, value)
                || cn.hutool.core.lang.Validator.isMatchRegex(PatternPool.TEL, value)
                || cn.hutool.core.lang.Validator.isMatchRegex(PatternPool.TEL_400_800, value);
    }

}
